#include "loadingDialog.h"
#include "loadingthread.h"

LoadingDialog::LoadingDialog(QDialog *parent): QDialog(parent),
ui(new Ui::Dialog)
{
    ui->setupUi(this);
    QObject::connect(this,SIGNAL(finished(int)),this,SLOT(resetDialog()));
    lt = 0;

}
LoadingDialog::~LoadingDialog() {
    lt->stop();
    while(!lt->isFinished()) {
        usleep(100);
    }
    delete ui;
    delete lt;
}
void LoadingDialog::load(vCardHash *contacts, QByteArray memory, QMobex *phone) {
    lt = new LoadingThread(phone);
    QObject::connect(lt,SIGNAL(parsed(int)),this,SLOT(setValue(int)));
    QObject::connect(lt,SIGNAL(total(int)),this,SLOT(setTotal(int)));
    QObject::connect(this,SIGNAL(rejected()),lt,SLOT(stop()));
    QObject::connect(lt,SIGNAL(finished()), this,SLOT(accept()));
    lt->load(contacts, memory);
    lt->start();
}

void LoadingDialog::setTotal(int total) {
    ui->progressBar->setRange(0, total);
}
void LoadingDialog::setValue(int value){
    ui->progressBar->setValue(value);
}
void LoadingDialog::setText(QString text) {
    ui->label->setText(text);
}

void LoadingDialog::resetDialog() {
    setTotal(0);
    setValue(1);
    setText(tr("Loading....."));
}
